package com.jinbei.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinbei.common.Enum.ApproverEnum;
import com.jinbei.common.annotation.AuthorAnnotation;
import com.jinbei.domain.PayOrder;
import com.jinbei.domain.UserGold;
import com.jinbei.mapper.PayOrderMapper;
import com.jinbei.service.PayOrderService;
import com.jinbei.service.UserGoldService;
import org.sojex.gold.enums.GoldLogType;
import org.sojex.gold.model.GoldUpdateParam;
import org.sojex.gold.remote.HessianGoldServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zflu
 * Created by lucky on 2018/9/17.
 */
@Service
public class PayOrderServiceImpl implements PayOrderService {

    @Autowired
    private PayOrderMapper payOrderMapper;

    @Autowired
    private UserGoldService userGoldService;

    @Autowired
    private HessianGoldServer hessianGoldServer;

    /**
     * @author zflu
     * @Description   元宝充值订单新增
     * @date 2018/9/17 16:06
     * @param payOrder
     * @return int
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int insertPayOrder(PayOrder payOrder) throws Exception {
        int payResult = payOrderMapper.insertPayOrder(payOrder);
        int result = 0;
        if(payResult > 0){
            List<UserGold> userGolds = payOrder.getUserGolds();
            userGolds.forEach(info->info.setPayId(payOrder.getId()));
            result = userGoldService.insertUserGoldList(userGolds);
        }
        return result;
    }

    /**
     * @author zflu
     * @Description 用户元宝充值订单审批
     * @date 2018/9/17 16:44
     * @param payOrder
     * @param request
     * @return int
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int approverPayOrder(PayOrder payOrder , HttpServletRequest request) throws Exception {
        int result = payOrderMapper.approverPayOrder(payOrder);
        //如果审批通过
        if(result > 0 && ApproverEnum.SUCCESS_APPROVER.getCode().equals(payOrder.getStatus())){
            //调用元宝hessian 充值
            List<GoldUpdateParam> goldUpdateParams = new ArrayList<>();
            List<UserGold> userGolds = userGoldService.queryUserGoldList(payOrder.getId());
            userGolds.forEach(info->{
                GoldUpdateParam param = new GoldUpdateParam();
                param.setUid(info.getUid());
                param.setGoldAmount(info.getGoldAmount());
                param.setGoldLogType(GoldLogType.RECHARGE);
                param.setIp(request.getRemoteAddr());
                goldUpdateParams.add(param);
            });
            hessianGoldServer.updateGoldBatch(goldUpdateParams);
        }
        return result;
    }

    /**
     * @author zflu
     * @Description   用户元宝充值订单删除
     * @date 2018/9/17 16:49
     * @param id
     * @return int
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int deletePayOrder(Integer id) throws Exception {
        int result = 0;
        int payResult = payOrderMapper.deletePayOrderById(id);
        if(payResult > 0){
            result = userGoldService.deleteUserGoldByPayId(id);
        }
        return result;
    }

    /**
     * @author zflu
     * @Description  用户元宝充值订单详情
     * @date 2018/9/17 17:10
     * @param id
     * @return int
     */
    @Override
    public PayOrder queryPayOrderById(Integer id) throws Exception{
        PayOrder payOrder = payOrderMapper.queryPayOrderById(id);
        if(payOrder != null){
            List<UserGold> userGolds = userGoldService.queryUserGoldList(id);
            payOrder.setUserGolds(userGolds);
        }
        return payOrder;
    }

    /**
     * @author zflu
     * @Description  元宝充值订单列表
     * @date 2018/9/17 17:57
     * @param pageNum
     * @param pageSize
     * @return com.github.pagehelper.PageInfo<com.jinbei.domain.PayOrder>
     */
    @Override
    public PageInfo<PayOrder> queryPayOrderPage(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum,pageSize);
        List<PayOrder> payOrders = payOrderMapper.queryPageOrderList();
        return new PageInfo<>(payOrders);
    }
}
